<?php
namespace Api\Controller;

use Think\Controller;

class AddressController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 收货地址
     * @return [type] [description]
     */
    public function index()
    {
        $adds = D('Api/Address', 'Service')->lists(USER_ID);
        success($adds);
    }

    /**
     * 新增收货地址
     */
    public function add()
    {
        $data = [
            'mobile'   => $mobile = I('post.mobile', '', 'trim'),
            'name'     => $name = I('post.name', '', 'trim'),
            'province' => $province = I('post.province', '', 'trim'),
            'city'     => $city = I('post.city', '', 'trim'),
            'region'   => $region = I('post.region', '', 'trim'),
            'address'  => $address = I('post.address', '', 'trim'),
        ];
        $is_default = I('post.is_default', false, 'boolval');

        if (!$mobile || !is_phone($mobile)) {
            error(8008, '手机号码格式错误');
        }

        if (!$name) {
            error(8009, '收货人姓名不能为空');
        }

        if (!$province && !$city && !$region && !$address) {
            error(8010, '收货地址不能为空');
        }

        $res = D('Api/Address', 'Service')->add(USER_ID, $data, $is_default);

        if ($res) {
            success();
        } else {
            error(8011, '添加收货地址失败');
        }
    }

    /**
     * 删除收货地址
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('post.id', 0, 'intval');

        $res = D('Api/Address', 'Service')->delete(USER_ID, $id);

        if ($res) {
            success();
        } else {
            error(8012, '删除收货地址失败');
        }
    }

    /**
     * 设置默认收获地址
     */
    public function setDefault()
    {
        $id = I('post.id', 0, 'intval');

        $res = D('Api/Address', 'Service')->setDefault(USER_ID, $id);

        if ($res) {
            success();
        } else {
            error(8013, '设置默认收货地址错误');
        }
    }

    /**
     * 设置默认收获地址
     */
    public function getDefault()
    {
        $add = D('Api/Address', 'Service')->getDefault(USER_ID);

        success($add);
    }

    /**
     * 更新收货地址
     * @return [type] [description]
     */
    public function update()
    {
        $id   = I('post.id', 0, 'intval');
        $data = [
            'mobile'     => $mobile = I('post.mobile', '', 'trim'),
            'name'       => $name = I('post.name', '', 'trim'),
            'province'   => $province = I('post.province', '', 'trim'),
            'city'       => $city = I('post.city', '', 'trim'),
            'region'     => $region = I('post.region', '', 'trim'),
            'address'    => $address = I('post.address', '', 'trim'),
            'is_default' => $is_default = I('post.is_default', 0, 'intval'),
        ];

        if (!$mobile || !is_phone($mobile)) {
            error(8008, '手机号码格式错误');
        }

        if (!$name) {
            error(8009, '收货人姓名不能为空');
        }

        if (!$province && !$city && !$region && !$address) {
            error(8010, '收货地址不能为空');
        }

        $res = D('Api/Address', 'Service')->update(USER_ID, $id, $data);
        if ($res) {
            success();
        } else {
            error(8014, '修改收货地址错误');
        }
    }
}
